#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tcpSocket = new QTcpSocket(this);
    isRunning=false;
    ui->start->setVisible(false);

    connect(tcpSocket,&QTcpSocket::connected,[=](){
        cout <<"connect phone successfully!"<<endl;
    });

    connect(tcpSocket,&QTcpSocket::readyRead,[=](){
        QByteArray buf = tcpSocket->readAll();
//        cout <<"recv:"<<buf.size()<<endl;
        int start=0;
        int index=0;
        char *data=buf.data();
        while(1){
            index=buf.indexOf("#*#",start);
            if(index==-1)
            {
                QByteArray temp(&data[start],buf.size()-start);
                recvMess(temp);
                break;
            }
            else
            {
                QByteArray temp(&data[start],index-start);
                recvMess(temp);
            }
            start=index+3;
        }
    });

    for(int i=0;i<MAX_PIC;i++)
        recvBuf[i]=QByteArray(0,'c');
    recvPlace=0;
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::recvMess(QByteArray buf)
{
    char *data=buf.data();
    if(cmp_str(data,"size:"))
    {
        pw=100,ph=100;
        sscanf(&data[5],"%d,%d,%lf",&pw,&ph,&image_scale);
        cout <<pw<<" "<<ph<<" "<<image_scale<<endl;

        this->setMaximumHeight((int)(ph/image_scale));
        this->setMinimumHeight((int)(ph/image_scale));
        this->setMaximumWidth((int)(pw/image_scale));
        this->setMinimumWidth((int)(pw/image_scale));

        char temp[100]="first";
        tcpSocket->write(temp,10);

    }
    else if(cmp_str(data,"img"))
    {
        int size=0;
        sscanf(&data[3],"%d",&size);
        recvBuf[recvPlace].clear();
        time.start();
    }
    else if(cmp_str(data,"endImg"))
    {
        pic=QImage((uchar *)recvBuf[recvPlace].data(),pw,ph,QImage::Format_RGB32);
        this->update();
        recvPlace=(recvPlace+1)%MAX_PIC;

        char temp[100]="start";
        tcpSocket->write(temp,10);

        ui->label->setText(QString::number(time.elapsed())+tr("ms"));
    }
    else if(cmp_str(data,"endData"))
    {
        QByteArray arr=qUncompress(recvBuf[recvPlace]);
        QImage temp(pic);
        uchar *last=temp.bits();
        uchar *data1=(uchar *)arr.data();
        int size=arr.size();
        for(int i=0;i<size;i++)
        {
            last[i]=last[i]+data1[i];
        }
        pic=QImage(temp);
        this->update();
        recvPlace=(recvPlace+1)%MAX_PIC;
        ui->label->setText(QString::number(time.elapsed())+tr("ms"));
    }
    else
    {
        recvBuf[recvPlace].append(buf);
    }
}

void MainWindow::paintEvent(QPaintEvent *) {

    if(pic.width()!=0)
    {
        QRectF target(0, 0, this->width(), this->height());
        QRectF src(0, 0, pic.width(), pic.height());
        QPainter painter1(this);
        painter1.drawImage(target, pic, src);
//        cout <<"over:paintEvent"<<endl;
    }

}

void MainWindow::keyPressEvent(QKeyEvent *ev)
{
    if(ev->key()==Qt::Key_Q&&!ev->isAutoRepeat())
    {
        if(isRunning)
        {
            char temp[100]="stop";
            tcpSocket->write(temp,10);
            ui->start->setVisible(true);
            isRunning=false;
        }
    }
    else if(ev->key()==Qt::Key_B)
    {
        if(isRunning)
        {
            int w=this->width();
            int h=this->height();
            this->setMaximumHeight(h+2);
            this->setMinimumHeight(h+2);
            this->setMaximumWidth(w+2);
            this->setMinimumWidth(w+2);
        }
    }
    else if(ev->key()==Qt::Key_S)
    {
        if(isRunning)
        {
            int w=this->width();
            int h=this->height();
            this->setMaximumHeight(h-2);
            this->setMinimumHeight(h-2);
            this->setMaximumWidth(w-2);
            this->setMinimumWidth(w-2);
        }
    }
}

void MainWindow::on_start_clicked()
{
    char temp[100]="size";
    tcpSocket->write(temp,10);

    isRunning=true;
    ui->start->setVisible(false);
}

void MainWindow::on_btn_clicked()
{
    QHostAddress test;
    QString ip=ui->input->text();
    if (test.setAddress(ip))
    {
        tcpSocket->connectToHost(QHostAddress(ip),8543);
        if(tcpSocket->isOpen())
        {
            ui->input->setVisible(false);
            ui->btn->setVisible(false);
            ui->start->setVisible(true);
        }
        else
        {
            ui->input->setText(tr("connect failed!"));
        }
    }
    else
        ui->input->setText(tr("ip address is wrong!"));
}
